﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：李弼翀
    //
    // 文件名：HospitalAccessAuthorize.aspx.cs
    // 文件功能描述：根据搜索条件找出满足条件的医疗机构信息，并决定选中的医疗机构是否可以入围
    // 创建标识：李弼翀 2009-03-16
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Query;
using System.Collections.Generic;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class HospitalAccessAuthorize : BasePage
    {

        //
        IBLL_User iuser = Factory.BLL_User();
        IBLL_OperatingRule operateRuleCtrl = Factory.BLL_OperatingRule();

        /// <summary>
        /// 目录入围权限ID
        /// </summary>
        protected string _operatingRuleID = "";

        public HospitalAccessAuthorize()
        {
            this.ModuleNo = Module.医疗机构授权管理.ToString();
            this.PageOperateType = OperateType.医疗机构勾选目录授权.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取目录入围对应的权限ID
            OperatingRule operateRule = operateRuleCtrl.GetOperatingRule(Module.制定采购目录, OperateType.入围);
            if (operateRule != null)
            {
                _operatingRuleID = operateRule.OperatingRuleID;
            }

            if (!IsPostBack)
            {

                StoreCondition();
                //此处调用目的，为了设置DropdownList的状态
                GetDefaultAreaString();
                //查询
                setBind();
            }
        }


        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            StoreCondition();
            setBind();
        }

        /// <summary>
        /// 定义绑定数据的方法
        /// </summary>
        private void setBind()
        {
            try
            {
                //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
                int totalRecord = 0;
                HZ.DrugTrade.Model.Query.HospitalCatalogManageRule pu = (HZ.DrugTrade.Model.Query.HospitalCatalogManageRule)ViewState["v_user"];
                IList<HZ.DrugTrade.Model.View.HospitalCatalogManageRule> mhrList = iuser.GetHospitalCatalogManageRulesByWhere(pu, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);

                //转换成DataTable
                this.gvwHospitalList.DataSource = mhrList;
                AspNetPager1.RecordCount = totalRecord;
                this.gvwHospitalList.DataBind();
            }
            catch (Exception ex)
            {
                PageError("数据连接失败！" + ex.Message, "");
            }
        }

        /// <summary>
        /// 存储查询条件
        /// </summary>
        private void StoreCondition()
        {
            HospitalCatalogManageRule aHospitalUsers = new HospitalCatalogManageRule();
            //入围权限ID
            aHospitalUsers.OperatingRuleID = this._operatingRuleID;
            aHospitalUsers.HospitalName = txtHospitalName.Text.Trim();
            aHospitalUsers.AreaID = GetAreaID();
            aHospitalUsers.SortFields = " HospitalID asc ";
            ViewState["v_user"] = aHospitalUsers;
        }

        /// <summary>
        /// 操作，开启授权
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEnableAccess_Click(object sender, EventArgs e)
        {
            string strUserIDs = Request["chkUser"];//获取选中的用户ID，形式如 01,02,03
            if (!string.IsNullOrEmpty(strUserIDs))
            {
                int nSuccess = iuser.AuthorizeHospitalCatalogManageRule(strUserIDs, this._operatingRuleID);
                if (nSuccess == 0)
                {
                    PageError("授权失败！", "");
                }
                else if (nSuccess == 1)
                {
                    PageInfo("授权成功！", "");
                }
            }
        }

        /// <summary>
        /// 操作，关闭授权
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDisableAccess_Click(object sender, EventArgs e)
        {
            string strUserIDs = Request["chkUser"];//获取选中的用户ID，形式如 01,02,03
            if (!string.IsNullOrEmpty(strUserIDs))
            {
                int nSuccess = iuser.RevokeHospitalCatalogManageRule(strUserIDs, "230");
                if (nSuccess == 0)
                {
                    PageError("操作失败！", "");
                }
                else if (nSuccess == 1)
                {
                    PageInfo("操作成功！", "");
                }
            }
        }

        #region 获取地区信息
        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                }

            }
            return areaId;
        }
        #endregion

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "320000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }
    }
}
